package handler

import (
	"encoding/json"
	"github.com/xiyoufang/cloud-syncer/internal/env"
	"github.com/xiyoufang/cloud-syncer/internal/http/model"
	"net/http"
)

func StatisticsHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var req model.PaginateReq
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		env.Logger().Error(err.Error())
		http.Error(w, "Invalid request payload", http.StatusBadRequest)
		return
	}
	last30dSyncStatistics, err := env.Repository().GetLast30dSyncStatistics()
	if err != nil {
		env.Logger().Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	response := model.ResponseBodyEntity{
		Code:    "OK",
		Message: "success",
		Data: model.StatisticsVO{
			Last30dSyncStatistics: last30dSyncStatistics,
		},
	}
	if err = json.NewEncoder(w).Encode(response); err != nil {
		env.Logger().Error(err.Error())
		http.Error(w, "Failed to encode response", http.StatusInternalServerError)
		return
	}
}
